package com.homegoods.orderinfodetail;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.homegoods.area.model.SubModelList;
import com.homegoods.classify.model.Constant;
import com.homegoods.orderinfodetail.bean.AddAddressParams;
import com.homegoods.orderinfodetail.bean.AddressListModel;
import com.homegoods.orderinfodetail.bean.ShoppingOrderdetail;
import com.homegoods.orderinfodetail.model.AddressModel;
import com.homegoods.orderinfodetail.model.OrderInfoDetailModel;
import com.homegoods.orderinfodetail.model.OrderPayBean;
import com.homegoods.resource.HomeGoodsApp;
import com.network.retrofit_rx.Api.ActionSubscriber;
import com.network.retrofit_rx.Api.CommandImpl;
import com.network.retrofit_rx.Api.HttpManager;

import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class OrderInfoApiUtils {
    /**
     * 添加订单前获取的订单详情
     *
     * @param error cmd=PreparedPay     //固定传的方法名
     *              outdata=jsonlist    //数据传输方式
     *              token=000           //身份token验证
     *              Addresid=ooo        //地址的sid
     *              ocean=66            //大洋数量
     *              content=[{sid:'商品sid',serve:'上门服务',num:'商品数量',stadarry:'属性sid集合,传字符数组',argument:'属性的中文名称'}]         //商品参数拼接成json字符的数组
     */
    public static void getOrderDetails(String content, String addresid, Consumer<OrderPayBean> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.GET_ORDER_LIST)
                .addParams("cmd", "PreparedPay")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .addParams("Addresid", addresid)
                .addParams("ocean", "")
                .addParams("content", content)
                .setObject(true)
                .setClass(OrderPayBean.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 添加收货地址
     * <p>
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>GET>>>>>>>>>>>>>>>>>>>>>>
     * token=1784378e05f94669b5acb9d31205eda2
     * cmd=adit
     * <<<<<<<<<<<<<<<<<<<<<<<<<<<GET<<<<<<<<<<<<<<<<<<<<<<
     * <p>
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>POST>>>>>>>>>>>>>>>>>>>>>
     * sid=                                     //记录ID
     * Title=我认为你认为的可能不对             //详细地址
     * Contacts=测试                            //联系人
     * MPhone=15800327113                       //联系电话
     * AreaSid=                                 //行政地区
     * AddrCode=003399                          //邮编
     * IsDefault=0                              //默认
     */
    /*public static void setArea(AddressModel model, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.USER_ADDR_API)
                .addParams("cmd", "adit")
                .addParams("token", HomeGoodsApp.getToken())
                .addField("Title", model.getTitle())
                .addField("Contacts", model.getContacts())
                .addField("MPhone", model.getMPhone())
                .addField("AreaSid", model.getAreaSid())
                .addField("AddrCode", "")
                .addField("sid", model.getSid())
                .addField("IsDefault", model.getIsDefault())
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }*/

    public static void setArea(AddAddressParams params, Consumer<String> consumer, Consumer<Throwable> error) {
        HttpManager.getInstance().post(Constant.SERVER_URL + Constant.USER_ADDR_API,
//                "Bearer " + HomeGoodsApp.getToken(),
                JSON.toJSONString(params),
                new ActionSubscriber<>(consumer, error, null),
                String.class);
    }

    /**
     * 删除收货地址
     * token=111
     * cmd=Modification   //方法名
     * out=jsonlist       //数据传输方式
     * type=5            //type固定传5，不能传别的
     * sid=ppp          //地址的sid
     */
    public static void deleteCityLisItem(String sid, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.USER_ADDR_API)
                .addParams("cmd", "delt")
                .addParams("token", HomeGoodsApp.getToken())
                .addField("sid", sid)
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取收货地址
     * AppType=useraddr_&outdata=json
     * token=b230e22fcf634cf8afb63e52ea8a8bdf   //登录令牌
     * psize=50
     * pos=1
     */
    /*public static void getCityList(Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CITY_LIST)
                .addParams("AppType", "useraddr_")
                .addParams("outdata", "json")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("psize", "10")
                .addParams("pos", "1")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }*/

    public static void getCityList(int pageNum, int pageSize, Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CITY_LIST + "/" + pageNum + "/" + pageSize)
//                .addParams("pageNum", 1)
//                .addParams("pageSize", 10)
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }


    /**
     * 获取商品详情
     * http://full-link.cn/amapi/SimplePlanCmd.aspx?
     * cmd=CommodityInf
     * &data=Efa%2bDmrErSJp72vG%2b6niaYfV1KUC4VrA81QsjkImVNQSVsK5eA4JtngfNmCTpIjS
     * &token=cbbe0ac7eabc4788a16f6a99a985e166
     */
    public static void getCommodityDetails(String sid, Consumer<OrderInfoDetailModel> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.SIMPLE_PLAN_CMD)
                .addParams("data", sid)
                .addParams("cmd", "CommodityInf")
                /*.addParams("token", HomeGoodsApp.getToken())*/
                .setObject(true)
                .setClass(OrderInfoDetailModel.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取店铺详情
     * <p>
     * AppType=ShopDetails_Bysid
     */
    public static void getShopDetails(String sid, Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLIST_CENTER)
                .addParams("AppType", "ShopDetails_Bysid" + sid)
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }


    /**
     * 获取店铺详情的商品列表
     * <p>
     * AppType=Bytype3_Bysid      //Bytype3固定这样传，不要变，Bysid后面接店铺的sid
     * cmd=Comdiylist             //
     * outdata=jsondata           //排序方式
     */
    public static void getShopDetailsCommodityList(String sid, Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.SIMPLE_PLAN_CMD)
                .addParams("AppType", "Bytype3_Bysid" + sid)
                .addParams("cmd", "Comdiylist")
                .addParams("outdata", "outdata")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取店铺详情商品规格
     * <p>
     * cmd=Product&comsid=Efa%2bDmrErSJp72vG%2b6niaYfV1KUC4VrA81QsjkImVNQSVsK5eA4JtngfNmCTpIjS&outdata=jsonlist
     */
    public static Observable<String> getCommoditySpecifications(String sid) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.AMAPI_SIMPLEPLANCMD)
                .addParams("outdata", "jsonlist")
                .addParams("cmd", "Stylename")
                .addParams("comsid", sid)
                .setObject(true)
                .setClass(String.class)
                .create();
        return HttpManager.getInstance().getObservable(command);
    }

    /**
     * 获取店铺详情的商品参数
     * <p>
     * cmd=Product&comsid=Efa%2bDmrErSJp72vG%2b6niaYfV1KUC4VrA81QsjkImVNQSVsK5eA4JtngfNmCTpIjS&outdata=jsonlist
     */
    public static Observable<String> getCommodityParameter(String sid) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.AMAPI_SIMPLEPLANCMD)
                .addParams("outdata", "jsonlist")
                .addParams("cmd", "Product")
                .addParams("comsid", sid)
                .setObject(true)
                .setClass(String.class)
                .create();
        return HttpManager.getInstance().getObservable(command);
    }

    /**
     * 添加商品到购物车
     * <p>
     * cmd=AddCollection      //方法名
     * outdata=jsonlist       //数据返回格式
     * token=000              //身份验证
     * comdiy=yyuuuu          //收藏物品的sid
     * type=0         //[购物车|0, 收藏|1, 店铺|2, 设计师|3]
     * shopdata="[{}]"  //添加购物车选的参数;数据格式：[{sid:'商品sid',serve:'上门服务',num:'商品数量',argument:'属性的名称',stadarry:[{title:'颜色',value:'黄色'}]}属性集合]
     * comnum=00     //数量，添加购物车时用
     * <p>
     * <p>
     * 、、
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>GET>>>>>>>>>>>>>>>>>>>>>>
     * token=cbbe0ac7eabc4788a16f6a99a985e166   //登录令牌
     * <<<<<<<<<<<<<<<<<<<<<<<<<<<GET<<<<<<<<<<<<<<<<<<<<<<
     * <p>
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>POST>>>>>>>>>>>>>>>>>>>>>
     * //【取消订单】=====================================
     * cmd=delt
     * sid=                                     //记录SID
     * //【添加订单】=====================================
     * cmd=adit
     * AutoOD=1、0                              //自建订单
     * Money=34.88                              //金额
     * NCount=3                                 //数量
     * CmdtSid=15800327113                      //商品SID
     * SChoice=                                 //选参
     * AboutInf=0                               //说明
     */
    public static void addCollectionShopCar(Map<String, Object> data, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CAPOOL_BUYRCDAPI)
                .addParams("outdata", "jsonlist")
                .addParams("token", HomeGoodsApp.getToken())
                .putField(data)
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>GET>>>>>>>>>>>>>>>>>>>>>>
     * token=5ff890aec06d40e28070076b  //登录令牌
     * cmd=adit                        //adit,delt,selt,csum
     * <<<<<<<<<<<<<<<<<<<<<<<<<<<GET<<<<<<<<<<<<<<<<<<<<<<
     * <p>
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>POST>>>>>>>>>>>>>>>>>>>>>
     * //adit 建立关系 需要token支持
     * BindType=                                //绑定类别
     * TerminatingCall=                         //被叫SID
     * AboutInf=                                //主叫说明
     * //delt 删除关系 需要token支持
     * sid=                                     //记录ID
     * //selt 查询关系 需要token支持
     * BindType=                                //绑定类别
     * TerminatingCall=                         //被叫SID
     * //csum 统计关系 无需token支持
     * BindType=                                //绑定类别
     * TerminatingCall=                         //被叫SID
     * <<<<<<<<<<<<<<<<<<<<<<<<<<<POST<<<<<<<<<<<<<<<<<<<<<
     * <p>
     * >>>>>>>>>>>>>>>>>>>>>>>>>>>OTHER>>>>>>>>>>>>>>>>>>>>
     * BindType 绑定类别 由前端统一标识即可如：
     * 新闻收藏：NewsCollectVol
     * 视频收藏：MVideoCollectVol
     * 商品收藏：CommodityCollectVol
     */
    public static void addVideoCollect(String sid, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CAPOOL_UABINDAPI)
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("cmd", "adit")
                .addField("BindType", "CommodityCollectVol")
                .addField("TerminatingCall", sid)
                .addField("AboutInf", "")
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }
    /**
     * 【084】预支付订单详情
     *
     * cmd = PrepareOrder
     * token=2b3caa5585864697b2b88fb71acf05cc //用户token
     * content=              //购物车sid组合，中间用","连接，去掉最后一个","。
     *
     */
    public static void getPrepareOrder(String content, Consumer<ShoppingOrderdetail> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.AMAPI_SIMPLEPLANCMD)
                .addParams("cmd", "PrepareOrder")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("content", content)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

}
